﻿@{
    Layout = "~/Views/Shared/_AddEdit.cshtml";
}
@section header{
    <script type="text/javascript" src="../Htc/json2.js"></script>
    <style>
        .poppanDiv input[type=text] {
            line-height: 25px;
            padding-left: 10px;
            height: 25px;
        }

        .poppanDiv select {
            line-height: 30px;
            height: 30px;
            padding-left: 10px;
        }

        .poppanDiv .imgBtn {
            cursor: pointer;
            margin-top: 10px;
            margin-left: 5px;
        }
    </style>
    <script>
        $(document).ready(function () {
            var id = "editExp";
            $("#" + id + " .RowData").bind("click", function () { EditRow(id, this); });
            $("#" + id + " .GridViewSelectCheckBox").bind("click", function (event) { event.stopPropagation(); });
        });
        function SelectAllRows(id, checked) {
            var color = checked ? "#DFE8F6" : "transparent";
            $("#" + id + " .GridViewSelectCheckBox").prop("checked", checked);
            if (checked) {
                $("#" + id + " .GridViewRow").addClass("GridViewSelect");
            }
            else {
                $("#" + id + " .GridViewRow").removeClass("GridViewSelect");
            }
            layui.form.render();
        }

        function AddRow(id) {
            ShowRow(id);
            $("#" + id).html($("#" + id).html() + "<tr  class='GridViewRow GridViewRow_0 RowData'>" + $(".RowTemplate").html() + "</tr>");
            $("#" + id + " .RowData").bind("click", function () { EditRow(id, this); });
            $("#" + id + " .GridViewSelectCheckBox").bind("click", function (event) { event.stopPropagation(); });
            $("#" + id + " .RowData:last").unbind("click");
            $("#" + id + " .GridViewSelectCheckBox:last").unbind("click");
            layui.form.render();
        }

        function EditRow(id, obj)	//编辑时取值
        {
            var values = new Array();
            var i = 0;
            var keyValue = $(obj).find(".GridViewSelectCheckBox").val();
            $(obj).find(".EditField").each(function () {
                values[$(this).attr("field")] = $(this).attr("value");
            });
            ShowRow(id);
            $("#" + id + " .RowData").bind("click", function () { EditRow(id, this); });
            $("#" + id + " .GridViewSelectCheckBox").bind("click", function (event) { event.stopPropagation(); });
            $(obj).unbind("click");
            $(obj).find(".GridViewSelectCheckBox").unbind("click");
            $(obj).html($("#" + id + " .RowTemplate").html());
            layui.form.render();
            $(obj).find(".GridViewSelectCheckBox").val(keyValue);

            $(obj).find(".EditField").each(function () {
                var value = values[$(this).attr("field")];
                $(this).attr("value", value);
                $(this).find("input").val(value);
                $(this).find("select").val(value);
            });
        }
        function DelRow(id)//删除
        {
            $("#" + id + " .GridViewSelectCheckBox:checked").parent().parent().hide();
        }
        function ShowRow(id)//显示 :并将编辑字段值保存
        {
            $("#" + id + " .RowData").unbind("click");
            $("#" + id + " .GridViewSelectCheckBox").unbind("click");
            $("#" + id + " .RowData .EditField").each(function () {
                var editField = $(this);
                editField.find("input").each(function () {
                    editField.attr("value", $(this).val());
                    editField.html($(this).val());
                });
                editField.find("select").each(function () {
                    editField.attr("value", $(this).val());
                    editField.html($(this).find("option[value='" + $(this).val() + "']").html());
                });
            });
        }
        function RowData(id)//获取数据
        {
            ShowRow(id);
            var data = [];
            var i = 0;
            var keyValue = $("#" + id).attr("keyValue");
            $("#" + id + " .RowData:visible").each(function () {
                var values = {};
                values[keyValue] = $(this).find(".GridViewSelectCheckBox").val();	//关键值
                $(this).find(".EditField").each(function () {
                    values[$(this).attr("field")] = $(this).attr("value");//其它值
                });
                data[i++] = values;
            });
            //删除的id
            var delId = "";
            $("#" + id + " .RowData:hidden .GridViewSelectCheckBox").each(function () {
                delId += "," + $(this).val();
            });
            if (delId != "") delId = delId.substring(1);
            $("#" + id).attr("delId", delId);

            return data;
        }
        function SaveBeforeCallBack() {
            var id = "editExp";
            var data = RowData(id);

            $("#_rowData").val(JSON.stringify(data));
            $("#_rowDataDel").val($("#" + id).attr("delId"));

            return false;
        }
    </script>
}

<div class="poppan">
    <table>
        <tr>
            <td>权限名称*</td>
            <td><input type="text" name="_scondname" value="@ViewBag.data["scondname"]" class="layui-input" /></td>
            <td>模块名称*</td>
            <td>
                <select name="_smodule">
                    @foreach (var dr in ViewBag.dict["smodule"])
                    {
                        <option value="@dr.Key" @(dr.Key == ViewBag.data["smodule"].ToString() ? "selected" : "")>@dr.Value</option>
                    }
                </select>
            </td>
        </tr>
        <tr>
            <td>用户名称</td>
            <td>
                <select name="_iuserid">
                    <option value=""></option>
                    @foreach (var dr in ViewBag.dtUser.Rows)
                    {
                        <option value="@dr["key"]" @(dr["key"].ToString() == ViewBag.data["iuserid"].ToString() ? "selected" : "")>@dr["value"]</option>
                    }
                </select>
            </td>
            <td>角色名称</td>
            <td>
                <select name="_srolecode">
                    <option value=""></option>
                    @foreach (var dr in ViewBag.dtRole.Rows)
                    {
                        <option value="@dr["key"]" @(dr["key"] == ViewBag.data["srolecode"].ToString() ? "selected" : "")>@dr["value"]</option>
                    }
                </select>
            </td>
        </tr>
    </table>
</div>

<div class="poppanDiv">
    <input type="hidden" name="_rowData" id="_rowData" />
    <input type="hidden" name="_rowDataDel" id="_rowDataDel" />
    <table class="GridViewTable" id="editExp" keyValue="id">
        <tr class="GridViewHeader">
            <td class="GridViewCheckBox"><input type="checkbox" lay-filter="checkAll" lay-skin="primary" alt="选择所有行" /></td>
            <td class="GridViewHeaderField" style="width:100px;">字段名称</td>
            <td class="GridViewHeaderField">运算符</td>
            <td class="GridViewHeaderField" style="width:330px;">值</td>
            <td class="GridViewHeaderField">逻辑符</td>
        </tr>
        <tr class="RowTemplate">
            <td><input class="GridViewSelectCheckBox" type="checkbox" value="-1" lay-skin="primary" /></td>
            <td class="EditField" field="sfieldname"><input type=text style="width:100px;" /></td>
            <td class="EditField" field="soperator">
                <select>
                    <option value="=">=</option>
                    <option Value=">">></option>
                    <option Value="<"><</option>
                    <option Value=">=">>=</option>
                    <option Value="<="><=</option>
                    <option Value="!=">!=</option>
                    <option Value="in">in</option>
                    <option Value="not in">not in</option>
                    <option Value="is">is</option>
                    <option Value="like">like</option>
                    <option Value="not like">not like</option>
                </select>
            </td>
            <td class="EditField" field="svalue"><input type=text style="width:330px;" /></td>
            <td class="EditField" field="slogic">
                <select>
                    <option value="and">与</option>
                    <option value="or">或</option>
                </select>
            </td>
        </tr>
        @foreach (var dr in ViewBag.dtCondExp.Rows)
        {
            <!--数据显示-->
            <tr class="GridViewRow GridViewRow_0 RowData">
                <td><input class="GridViewSelectCheckBox" type="checkbox" value="@dr["id"]" lay-skin="primary" /></td>
                <td class="EditField" field="sfieldname" value="@dr["sfieldname"]">@dr["sfieldname"]</td>
                <td class="EditField" field="soperator" value="@dr["soperator"]">@dr["soperator"]</td>
                <td class="EditField" field="svalue" value="@dr["svalue"]">@dr["svalue"]</td>
                <td class="EditField" field="slogic" value="@dr["slogic"]">
                    @dr["slogic_cn"]
                </td>
            </tr>
        }
    </table>
    <img src="../Skin/Plane/images/b01.png" onclick="AddRow('editExp')" class="imgBtn" />
    <img src="../Skin/Plane/images/b04.png" onclick="DelRow('editExp')" class="imgBtn" />
</div>

@section fooler{
<script>
    layui.use(['element', 'table', 'layer', 'laypage', 'form', 'laydate'], function () {
        layui.form.on('checkbox(checkAll)', function (data) {
            SelectAllRows('editExp', data.elem.checked);
        });
    });
</script>
}